﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Net5ApiAndEfMySql.API.Controllers;
using Net5ApiAndEfMySql.DtoModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VueAndNetCoreEfApi.IService.SystemAdmin;
using VueAndNetCoreEfApi.PoModel.departmentyrh;

namespace VueAndNetCoreEfApi.Controllers
{
    public class departmentController : BaseApiController
    {
        private IdepartmentService _idepartmentService;

        public departmentController(IdepartmentService idepartmentService)
        {
            _idepartmentService = idepartmentService;
        }
        [AllowAnonymous]
        [HttpPost]
        /// <summary>
        /// 部门管理显示
        /// </summary>
        /// <param name="sm"></param>
        /// <returns></returns>
        public ResultModel GetdepartmentList(SearchModel sm)
        {
            ResultModel result = new ResultModel();
            if (sm == null)
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            try
            {
                var data = _idepartmentService.GetdepartmentList(sm);
                data.draw = sm.draw;
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }
        [HttpPost]
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultModel departmentDelete(int id)
        {
            ResultModel result = new ResultModel();
            //if (id != null)
            //{
            //    result.code = ResultCode.Error;
            //    result.mess = "参数错误";
            //    return result;
            //}
            try
            {
                var data = _idepartmentService.departmentDelete(id);
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }
        [HttpPost]
        public ResultModel departmentAdd(department user)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(user.dp_name))
            {
                result.code = ResultCode.Error;
                result.mess = "必须输入用户名";
                return result;
            }
            try
            {
                var resultData = _idepartmentService.departmentAdd(user);
                if (resultData)
                {
                    result.code = ResultCode.Ok;
                    result.data = resultData;
                    result.mess = "成功!";
                }
                else
                {
                    result.code = ResultCode.Fail;
                    result.data = resultData;
                    result.mess = "添加失败!";
                }

            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }

        public ResultModel departmentFantian(int id)
        {
            ResultModel result = new ResultModel();
            //if (string.IsNullOrWhiteSpace(dp_name))
            //{
            //    result.code = ResultCode.Error;
            //    result.mess = "参数错误";
            //    return result;
            //}
            try
            {
                var data = _idepartmentService.departmentFantian(id);
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }
        public ResultModel departmentUpdate(department user)           
        {
            ResultModel result = new ResultModel();
            try
            {
                var resultData = _idepartmentService.departmentUpdate(user);
                result.code = ResultCode.Ok;
                result.data = resultData;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }
        
    }
}
   